package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.entity.Album;
import com.guoguo.entity.UserExt;
import com.guoguo.entity.Users;
import com.guoguo.entity.vo.UsersVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.service.IUserExtService;
import com.guoguo.service.IUsersService;
import com.guoguo.service.UploadService;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.FileUtils;
import com.guoguo.utils.StringUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/11 0011.
 */
@Controller
@RequestMapping("/view")
public class UserExtController extends BaseController {
    private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(UserExtController.class);
    @Resource
    private IUserExtService iuserExtService;
    @Resource
    private IUsersService iusersService;
    @Resource
    private UploadService uploadService;

    /**
     * 根据Id去查询用户的扩展信息
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/toUpdateUserExt", method = RequestMethod.GET)
    public ModelAndView User(String userId) {
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        Map<String, Object> data = new HashedMap();
        UserExt userExt = iuserExtService.findUserExt(userId);
        data.put("userId", userExt.getUserId());
        data.put("iconLocal", AliOssUtils.getImageUrl(userExt.getIcon()));//, aliOssImageConfig));
        data.put("icon", userExt.getIcon());
        data.put("email", userExt.getEmail());
        data.put("sex", userExt.getSex());
        data.put("phone", userExt.getPhone());
        return returnView("/user/userExtUpdate", data);

    }

    /**
     * 修改用户扩展信息
     *
     * @param userExt
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateUserExt", method = RequestMethod.GET)
    public JSONObject updateUsers(UserExt userExt, String icon) {
        Users users = new Users();
        Map<String, Object> data = new HashedMap();
        Date now = new Date();
        userExt.setUpdateTime(now);
        userExt.setIcon(icon);
        iuserExtService.updateUserExt(userExt);
        users.setUserId(userExt.getUserId());
        users.setPhone(userExt.getPhone());
        iusersService.updateUsers(users);
        return returnResult(200, data, "");
    }

    /**
     * 根据Id去禁言
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/updateUserExtJY", method = RequestMethod.GET)
    public ModelAndView updateUserExtJY(String userId,@RequestParam int pageNumber, @RequestParam int pageSize) {
        int i = iuserExtService.updateUserExtJY(userId);
        Map<String, Object> data = new HashedMap();

        PageBean<UsersVO> userList = iusersService.selectUserList(pageNumber, pageSize);
        data.put("userList", userList);
        return returnView("/user", data);
    }

    /**
     * 根据Id取消禁言
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/updateUserExtQXJY", method = RequestMethod.GET)
    public ModelAndView updateUserExtQXJY(String userId, @RequestParam int pageNumber, @RequestParam int pageSize) {
        int i = iuserExtService.updateUserExtQXJY(userId);
        Map<String, Object> data = new HashedMap();
        PageBean<UsersVO> userList = iusersService.selectUserList(pageNumber, pageSize);
        data.put("userList", userList);
        return returnView("/user", data);
    }

    /**
     * 头像上传
     *
     * @param fileName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadIcon", method = RequestMethod.POST)
    public JSONObject uploadIcon(HttpServletRequest request, @RequestParam(value = "file", required = false) String fileName, @RequestParam int isZip) {

        List<String> list = FileUtils.springFileUpload(request, isZip);
        List<String> ossFile = uploadService.uploadFile(list);

        JSONObject jsonObject = new JSONObject();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        if (ossFile.size() > 0) {
            jsonObject.put("icon", AliOssUtils.getImageUrl(ossFile.get(0)));//, aliOssImageConfig));
            jsonObject.put("iconLocal", ossFile.get(0));
            logger.debug("=======>>>   ossFile path:" + ossFile.get(0));
        }
        return returnResult(200, jsonObject, "");
    }

}
